package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.CouponCode;
import com.gxa.pingan.service.CouponCodeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/couponCode")
@CrossOrigin
@Api(value = "兑换码的控制器", tags = "兑换码的接口的api")
public class CouponCodeController {

    @Autowired
    private CouponCodeService couponCodeService = null;

    /**
     * 添加
     *
     * @Author lirucong
     * @Date 18:42 2021/10/25
     */
    @ApiOperation("兑换码信息的添加")
    @PostMapping("/add")
    public Map addCpnCode(@RequestBody CouponCode couponCode) throws BusinessException {
        System.out.println(couponCode);
        couponCodeService.add(couponCode);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("0");
        return jsonResult.getDataMap();
    }


    /**
     * 发送兑换码
     *
     * @Author lirucong
     * @Date 18:52 2021/10/25
     */
    @ApiOperation("将兑换码发给用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "兑换码id", required = true,paramType = "Long"),
            @ApiImplicitParam(name = "content", value = "发送的内容", required = true,paramType = "Sting"),
            @ApiImplicitParam(name = "phone", value = "用户电话", required = true,paramType = "Sting")
    })
    @PostMapping("sendCode")
    public Map sendCpnCode(Long cid, String content, String phone) throws BusinessException {
        couponCodeService.addSendCode(phone,cid,content);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("发送成功");
        return jsonResult.getDataMap();
    }

    /**
     * 修改兑换码
     *
     * @Author lirucong
     * @Date 18:52 2021/10/25
     */
    @ApiOperation("修改兑换码信息")
    @PostMapping("/update")
    public Map updateCpnCode(@RequestBody CouponCode couponCode) throws BusinessException {
        couponCodeService.update(couponCode);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("修改成功");
        jsonResult.setCode("0");

        return jsonResult.getDataMap();
    }

    /**
     * 删除兑换码
     *
     * @Author lirucong
     * @Date 18:53 2021/10/25
     */
    @ApiOperation("删除兑换码")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需要删除的兑换码id",paramType = "Long")
    })
    public Map deleteCpnCode(@ApiParam(value = "id",name = "id",required = true) Long id) throws BusinessException {
        couponCodeService.deleteCpnCode(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    /**
     * 获取兑换码分页列表
     *
     * @Author lirucong
     * @Date 18:55 2021/10/25
     */
    @ApiOperation("获取兑换码分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",paramType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页的条数",paramType = "Integer")
    })
    @GetMapping("/getCpnCodeList")
    public Map getCpnCodeList(Integer page, Integer size) {
        List<CouponCode> data = couponCodeService.getList(page, size);
        Integer count = couponCodeService.getListCount();
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count",count);
        jsonResult.setData(data);
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("0");

        return jsonResult.getDataMap();
    }

    /**
     *
     * @Author  lirucong
     * @Date 9:55 2021/10/27
     */
    @ApiOperation("检查输入的手机号是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号",paramType = "query"),
    })
    @PostMapping("/checkPhone")
    public Map checkPhone(String phone) throws BusinessException {
        Boolean checkPhone = couponCodeService.checkPhone(phone);
        JsonResult jsonResult = new JsonResult();
        if (checkPhone) {
            jsonResult.setCode("0");
            jsonResult.setMsg("成功");
        } else {
            jsonResult.setCode("1");
            jsonResult.setMsg("未找到该用户，请检查手机号");
        }
        return jsonResult.getDataMap();
    }


}
